package com.a.module_storage

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.a.lib_ui.activity.PopWindowsActivity
import kotlin.math.roundToInt

class ProductItemAdapterKt(list: MutableList<ProductItem>) :
    BaseQuickAdapter<ProductItem, BaseViewHolder>(R.layout.storage_item_product, list) {
    init {
        addChildClickViewIds(R.id.iv_product_picture)
    }

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        super.onItemViewHolderCreated(viewHolder, viewType)
//        viewHolder.setGone(R.id.tv_quantity, true)

    }

    override fun convert(holder: BaseViewHolder, item: ProductItem) {
        val url = "" +
                item.productType + "/" + item.classTypeNo + "/" + item.productNo + "/" + item.productNo + "_1T.jpg"
        Glide.with(context)
            .load(url) //.fallback(R.drawable.no_picture2)
            .error(R.drawable.no_picture)
            .into(holder.getView(R.id.iv_product_picture))
        holder.setText(R.id.tv_productName, item.productName)
        holder.setText(R.id.tv_factory, "货号：" + item.factoryNo)
        val packages = item.packages
        try {
            holder.setText(R.id.tv_num, "数量：" + packages.toDouble().roundToInt() + item.unitName)
        } catch (e: Exception) {
            LogUtils.e(e)
            holder.setText(R.id.tv_num, "数量：" + packages + item.unitName)
        }
        holder.setText(R.id.tv_quantity,  item.salePackTranRate + "只/" + item.unitName)
        holder.setText(R.id.tv_productNo, "编码：" + item.productNo)
    }

    override fun setOnItemChildClick(v: View, position: Int) {
        super.setOnItemChildClick(v, position)
        val item = getItem(position)
        val url = "/" +
                item.productType + "/" + item.classTypeNo + "/" + item.productNo + "/" + item.productNo + "_1T.jpg"
        val bundle = Bundle()
        bundle.putInt("key", 3) //指定实现的case
        bundle.putString("url", url)
        val intent = Intent(context, PopWindowsActivity::class.java)
        intent.putExtras(bundle)
        context.startActivity(intent)
    }
}